import api from '@/config/api.js'
import UserForm from '@/common/UserForm.js'

export default function(obj) {
	let param = Object.assign({}, {
		url: '',
		data: {},
		method: 'POST',
		timeout: 60000,
		dataType: 'json',
		success: () => {},
		fail: () => {},
		complete: () => {},
	}, obj)

	UserForm.getToken(token => {
		let header = Object.assign({}, {
			'Content-Type': 'application/json',
			'user-token': token
		}, param.header)

		uni.request({
			url: api.baseUrl + param.url, //仅为示例，并非真实接口地址。
			data: param.data,
			header: header,
			method: param.method,
			timeout: param.timeout,
			dataType: param.dataType,
			success: (data, statusCode, header, cookies) => {
				param.success(typeof data.data === 'undefined' || typeof data.data.data === 'undefined' ? null : data.data.data,
					data, statusCode, header, cookies)
			},
			fail: (error) => {
				param.fail(error)
			},
			complete: () => {
				param.complete()
			},
		});
	})
}
